package com.whw.cbf_qimodazuoye.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.whw.cbf_qimodazuoye.R;
import com.whw.cbf_qimodazuoye.entity.CheckOrders;
import com.whw.cbf_qimodazuoye.entity.Hotel;
import com.whw.cbf_qimodazuoye.service.CheckOrderService;
import com.whw.cbf_qimodazuoye.service.HotelService;
import com.whw.cbf_qimodazuoye.utils.FaSongQingQiu;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class OrderXiangXiActivity extends AppCompatActivity {

   TextView textViewHotelName;
   TextView textViewHotelLabel;
   TextView textViewCheckorderPrice;
   TextView textViewCheckorderPosttime;
   TextView textViewCheckOrdersCheckInTime;
   TextView textViewCheckOrdersCheckOutTime;
   TextView textViewUserName;
   TextView textViewUserPhone;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_xiangxi);

        Bundle bundle = this.getIntent().getExtras();
        final String userId = bundle.getString("userId");
        final String userName = bundle.getString("userName");
        final String userIntegral = bundle.getString("userIntegral");
        final String checkOrdersId = bundle.getString("checkOrdersId");

        textViewHotelName = findViewById(R.id.orderxiangxi_hotelName);
        textViewHotelLabel = findViewById(R.id.orderxiangxi_hotelLabel);
        textViewCheckorderPrice = findViewById(R.id.orderxiangxi_checkorderPrice);
        textViewCheckorderPosttime = findViewById(R.id.orderxiangxi_checkorderPosttime);
        textViewCheckOrdersCheckInTime = findViewById(R.id.orderxiangxi_checkOrdersCheckInTime);
        textViewCheckOrdersCheckOutTime = findViewById(R.id.orderxiangxi_checkOrdersCheckOutTime);
        textViewUserName = findViewById(R.id.orderxiangxi_userName);
        textViewUserPhone = findViewById(R.id.orderxiangxi_userPhone);
        initCheckOrder(checkOrdersId);

        Button buttoncancelBooking = findViewById(R.id.cancelBooking);

        buttoncancelBooking.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelBooking(checkOrdersId,userId,userName,userIntegral);
            }
        });

        Button deleteBookingButton = findViewById(R.id.deleteBooking);
        deleteBookingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteBooking(checkOrdersId,userId,userName,userIntegral);
            }
        });

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home,  R.id.navigation_notifications)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
        navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.navigation_home:{
                        Bundle bundle = new Bundle();
                        bundle.putString("userId",userId);
                        bundle.putString("userName",userName);
                        bundle.putString("userIntegral",userIntegral);
                        Intent intent=new Intent(OrderXiangXiActivity.this,MainActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        return true;
                    }
                    case R.id.navigation_notifications:{
                        return true;
                    }
                    case R.id.navigation_dashboard:{
                        Bundle bundle = new Bundle();
                        bundle.putString("userId",userId);
                        bundle.putString("userName",userName);
                        bundle.putString("userIntegral",userIntegral);
                        Intent intent=new Intent(OrderXiangXiActivity.this,RecommendationActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        return true;
                    }

                }
                return false;
            }
        });
    }

    private void initCheckOrder(final String checkOrdersId){
        Retrofit retrofit = FaSongQingQiu.createRetrofit();
        CheckOrderService checkOrderService = retrofit.create(CheckOrderService.class);
        Call<ResponseBody> call = checkOrderService.selectCheckOrderBycheckOrderId(checkOrdersId);
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //请求处理,输出结果
                try {
                    JSONObject jsonObject = (JSONObject) JSON.parse(response.body().string());
                    String msg =jsonObject.getString("msg");
                    String result = jsonObject.getString("result");
                    String returnobj = jsonObject.getString("obj");
                    JSONObject obj = (JSONObject)JSON.parse(returnobj);

                    if (result.equals("true")){
                        String userId =obj.getString("userId");
                        CheckOrders checkOrders = new CheckOrders();
                        checkOrders.setCheckOrdersId(obj.getString("checkorderId"));
                        checkOrders.setCheckOrdersCheckInTime(obj.getString("checkorderCheckInTime"));
                        checkOrders.setHotelId(Integer.parseInt(obj.getString("hotelId")));
                        checkOrders.setUserId(Integer.parseInt(obj.getString("userId")));
                        checkOrders.setUserName(obj.getString("userName"));
                        checkOrders.setCheckOrdersCheckOutTime(obj.getString("checkorderCheckOutTime"));
                        checkOrders.setCheckOrdersStatus(obj.getString("checkorderStatus"));
                        checkOrders.setUserIdCard(obj.getString("userIdCard"));
                        checkOrders.setUserPhone(obj.getString("userPhone"));
                        checkOrders.setCheckOrdersrPrice(Double.parseDouble((obj.getString("checkorderPrice"))));
                        checkOrders.setCheckOrdersPosttime(obj.getString("checkorderPosttime"));
                        Hotel hotel = new Hotel();
                        String returnHotelobj = obj.getString("hotel");
                        JSONObject objHotel = (JSONObject)JSON.parse(returnHotelobj);
                        hotel.setHotelName(objHotel.getString("hotelName"));
                        hotel.setHotelPrice(Long.parseLong(objHotel.getString("hotelPrice")));
                        hotel.setHotelNumber(Integer.parseInt(objHotel.getString("hotelNumber")));
                        hotel.setHotelIndex(objHotel.getString("hotelIndex"));
                        hotel.setHotelCoverpic(objHotel.getString("hotelCoverpic"));
                        hotel.setHotelIndexpic(objHotel.getString("hotelIndexpic"));
                        hotel.setHotelStatus(Integer.parseInt(objHotel.getString("hotelStatus")));
                        hotel.setHotelLabel(objHotel.getString("hotelLabel"));
                        checkOrders.setHotel(hotel);
                        if (hotel.getHotelName()!=null){
                            textViewHotelName.setText(hotel.getHotelName());
                            TextPaint paint = textViewHotelName.getPaint();
                            paint.setFakeBoldText(true);
                        }
                        if (hotel.getHotelLabel()!=null){
                            textViewHotelLabel.setText(hotel.getHotelLabel());
                            TextPaint paint = textViewHotelLabel.getPaint();
                            paint.setFakeBoldText(true);
                        }
                        System.out.println(checkOrders.getCheckOrdersrPrice());
                        if (checkOrders.getCheckOrdersrPrice()!=null) {

                            textViewCheckorderPrice.setText(checkOrders.getCheckOrdersrPrice().toString());
                            TextPaint paint = textViewCheckorderPrice.getPaint();
                            paint.setFakeBoldText(true);
                        }
                        if (checkOrders.getCheckOrdersPosttime()!=null){
                        textViewCheckorderPosttime.setText(checkOrders.getCheckOrdersPosttime());
                            TextPaint paint = textViewCheckorderPosttime.getPaint();
                            paint.setFakeBoldText(true);
                        }
                        if (checkOrders.getCheckOrdersCheckInTime()!=null){
                        textViewCheckOrdersCheckInTime.setText(checkOrders.getCheckOrdersCheckInTime());
                            TextPaint paint = textViewCheckOrdersCheckInTime.getPaint();
                            paint.setFakeBoldText(true);
                        }
                        if (checkOrders.getCheckOrdersCheckOutTime()!=null){
                        textViewCheckOrdersCheckOutTime.setText(checkOrders.getCheckOrdersCheckOutTime());
                            TextPaint paint = textViewCheckOrdersCheckOutTime.getPaint();
                            paint.setFakeBoldText(true);
                        }
                        if (checkOrders.getUserName()!=null){
                        textViewUserName.setText(checkOrders.getUserName());
                            TextPaint paint = textViewUserName.getPaint();
                            paint.setFakeBoldText(true);
                        }
                        if (checkOrders.getUserPhone()!=null){
                        textViewUserPhone.setText(checkOrders.getUserPhone());
                            TextPaint paint = textViewUserPhone.getPaint();
                            paint.setFakeBoldText(true);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //JsonObject jsonObject = new JsonObject()
            }
            //请求失败时候的回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("连接失败");
            }
        });
    }

    private void deleteBooking(String checkOrdersId, final String userId, final String userName, final String userIntegral){
        Retrofit retrofit = FaSongQingQiu.createRetrofit();
        CheckOrderService checkOrderService = retrofit.create(CheckOrderService.class);
        Call<ResponseBody> call = checkOrderService.deleteCheckOrderBycheckOrderId(checkOrdersId);
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //请求处理,输出结果
                try {
                    JSONObject jsonObject = (JSONObject) JSON.parse(response.body().string());
                    String msg =jsonObject.getString("msg");
                    String result = jsonObject.getString("result");
                    String returnobj = jsonObject.getString("obj");
                    JSONObject obj = (JSONObject)JSON.parse(returnobj);

                    if (result.equals("true")){
                        Toast.makeText(OrderXiangXiActivity.this,"删除成功！",Toast.LENGTH_LONG).show();
                        Bundle bundle = new Bundle();
                        bundle.putString("userId",userId);
                        bundle.putString("userName",userName);
                        bundle.putString("userIntegral",userIntegral);
                        Intent intent=new Intent(OrderXiangXiActivity.this,AllOrderActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //JsonObject jsonObject = new JsonObject()
            }
            //请求失败时候的回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("连接失败");
            }
        });
    }

    private void cancelBooking(String checkOrdersId, final String userId, final String userName, final String userIntegral){
        Retrofit retrofit = FaSongQingQiu.createRetrofit();
        CheckOrderService checkOrderService = retrofit.create(CheckOrderService.class);
        Call<ResponseBody> call = checkOrderService.updatedCheckorderStatus(checkOrdersId,1);
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //请求处理,输出结果
                try {
                    JSONObject jsonObject = (JSONObject) JSON.parse(response.body().string());
                    String msg =jsonObject.getString("msg");
                    String result = jsonObject.getString("result");
                    String returnobj = jsonObject.getString("obj");
                    JSONObject obj = (JSONObject)JSON.parse(returnobj);

                    if (result.equals("true")){
                        Toast.makeText(OrderXiangXiActivity.this,"取消成功！",Toast.LENGTH_LONG).show();
                        Bundle bundle = new Bundle();
                        bundle.putString("userId",userId);
                        bundle.putString("userName",userName);
                        bundle.putString("userIntegral",userIntegral);
                        Intent intent=new Intent(OrderXiangXiActivity.this,AllOrderActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //JsonObject jsonObject = new JsonObject()
            }
            //请求失败时候的回调
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println("连接失败");
            }
        });
    }
}
